$(function () {
    //登录
    $("#loginForm").submit(function (event) {
        //阻止元素发生默认行为
        event.preventDefault();
        //loading(2);
        $.ajax({
            url:"/shop/customer/login",
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {
                if (data == 1){
                    window.location.href = "/shop/permission/commodity/allType"
                }else {
                    $.alertView("用户名或密码错误");
                }
            }
        })
    })

    //注册
    $("#registerForm").submit(function (event) {
        event.preventDefault();
        $.ajax({
            url:"/shop/customer/register",
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {
                if (data == 1){
                    $.alertView("注册成功!!!");
                    window.location.href = "/shop/index.jsp"
                }
            }
        })
    });

    //修改个人信息
    $("#updateForm").submit(function (event) {
        event.preventDefault();
        $.ajax({
            url:"/shop/customer/updateCustomer",
            type:"POST",
            data:$(this).serialize(),
            success:function (data) {
                if (data == 1){
                    $.alertView("修改成功!!!");
                    window.location.href = "/shop/permission/commodity/allType"
                }
            }
        })
    });



    //购买
    $("#buyBtn").click(function () {
        $.ajax({
            url:"/shop/permission/order/buy",
            type:"POST",
            data:{
                "num":$("#orderNum").text(),
                "id":$("#orderId").val()
            },
            success:function (data) {
                if (data == 1){
                    //跳转
                    window.location.href = "/shop/permission/order/toSuccess"
                }
            }
        })
    });


    //购物车结算
    $("#settlement").click(function () {
        $.ajax({
            url:"/shop/permission/order/settlement",
            data:{},
            type:"POST",
            success:function (data) {
                if (data == 1){
                    //跳转
                    window.location.href = "/shop/permission/order/toSuccess"
                }
            }
        })
    });
})


//TODO
function showCommodityByType(type) {
    window.location.href = "/shop/permission/commodity/byType?type="+type
}

//退出函数
function logout() {

    var json = {
        title:"无忧购提醒您",
        msg:"您确定要退出吗?",
        buttons:[
            { title:"确定",color:"red",click:function(){window.location.href = "/shop/customer/logout"} },
            { title:"取消" }
        ]
    }

    $.alertView(json);

    // var bool = confirm("您确定要退出吗?");
    // if (bool){
    //     window.location.href = "/shop/customer/logout"
    // }
}

//跳转注册页面
function toRegister() {
    window.location.href = "/shop/customer/toRegister"
}

//验证用户名
function checkUsername() {
    $.ajax({
        url:"/shop/customer/checkUsername",
        data: {"username":$("#registerUsername").val()},
        type: "POST",
        success:function (data) {
            if (data == 1){
                $("#usernameHint").removeClass("icon-cuowu iconfont")
                $("#usernameHint").addClass("iconfont icon-duigou")
                $("#usernameHint").css("color","green")
                $("#registerSubmint").removeClass("disabled")
            }else {
                $("#usernameHint").removeClass("icon-duigou iconfont")
                $("#usernameHint").addClass("iconfont icon-cuowu")
                $("#usernameHint").css("color","red")
                $("#registerSubmint").addClass("disabled")
            }
        }
    })
}

//验证密码
function checkPwd() {
    var psw = $("#registerPassword").val();
    var checkPwd = $("#registerCheckpsw").val()
    if (psw == checkPwd){
        $("#passwordHint").removeClass("icon-cuowu iconfont")
        $("#passwordHint").addClass("iconfont icon-duigou")
        $("#passwordHint").css("color","green")
        $("#registerSubmint").removeClass("disabled")
    }else {
        $("#passwordHint").removeClass("icon-duigou iconfont")
        $("#passwordHint").addClass("iconfont icon-cuowu")
        $("#passwordHint").css("color","red")
        $("#registerSubmint").addClass("disabled")
    }
}

//每天特价显示
function page(pageNow) {
    window.location.href = "/shop/permission/commodity/allType?pageNow="+pageNow
}

//跳转到商品详情页
function toCommodityDetail(id){
    window.location.href = "/shop/permission/commodity/toCommodityDetail?id="+id;
}

//更新用户信息
function toUpdateCustomer(id) {
    window.location.href = "/shop/customer/toUpdateCustomer?id="+id;
}

//添加购物车
function addCart(commodity_id) {
    //window.location.href = "/shop/permission/commodity/addCart?commodity_id=" + commodity_id
    var num = $("#commodityNum").val() < 1 ? 1 : $("#commodityNum").val();
    $.ajax({
        url:"/shop/permission/commodity/addCart",
        data:{"commodity_id":commodity_id,"num":num},
        success:function (data) {
            if (data == 1){
                $.alertView("添加购物车成功");
            }
        }
    })
}

//删除购物车
function deleteCart(id) {
     window.location.href = "/shop/permission/commodity/deleteCart?id="+id;
}


//跳转到我的购物车
function toMyCart() {
    window.location.href = "/shop//permission/commodity/toMyCart"
}

//搜索
function searchCommodity() {
    var type = $("#searchType").val();
    var name = $("#searchValue").val();
    window.location.href = "/shop/permission/commodity/byType?type="+type+"&name="+name;
}

//跳转到购买商品页面
function toBuy(id) {
    var num = $("#commodityNum").val();
    window.location.href = "/shop/permission/commodity/toBuy?id=" + id + "&num=" + num;
}




